﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuanLyThuoc
{
    class HoaDon
    {
        string maHoaDon;
        string maNhanVien;
        string maKhachHang;
        DateTime ngayLap;
        //decimal triGia;
        //string mathuoc;
        //string maThuoc;
        

        public string MaHoaDon
        {
            get { return maHoaDon; }
            set { maHoaDon = value; }
        }
        public string MaNhanVien
        {
            get { return maNhanVien; }
            set { maNhanVien = value; }
        }
        public string MaKhachHang
        {
            get { return maKhachHang; }
            set { maKhachHang = value; }
        }
        public DateTime NgayLap
        {
            get { return ngayLap; }
            set { ngayLap = value; }
        }
        //public decimal TriGia
        //{
        //    get { return triGia; }
        //    set { triGia = value; }
        //}

        public HoaDon()
        { }

        public HoaDon(string maHoaDon, string maNhanVien, string maKhachHang, DateTime ngayLap)
        {
            this.maHoaDon = maHoaDon;
            this.maNhanVien = maNhanVien;
            this.maKhachHang = maKhachHang;
            this.ngayLap = ngayLap;
            //this.triGia = triGia;
        }

        public void NhapHD()
        {
            Console.Write("\nNhap ma hoa don: ");
            maHoaDon = Console.ReadLine();
            Console.Write("Nhap ma nhan vien: ");
            maNhanVien = Console.ReadLine();
            Console.Write("Nhap ma khach hang: ");
            maKhachHang = Console.ReadLine();
            //Console.Write("Ngay lap hoa don: ");
            ngayLap = DateTime.Now;
            //Console.Write("Tri gia hoa don: ");
            //triGia = decimal.Parse(Console.ReadLine());
        }

        private decimal thanhTien()
        {
            List<ChiTietHoaDon> chiTietHD = DanhSachChiTietHD.TimTheoMa(maHoaDon);
            decimal tongTien = 0;
            foreach (ChiTietHoaDon item in chiTietHD) //List<ChiTietHoaDon> & ChiTietHoaDon???
            {
                tongTien += item.SoLuong * DanhSachThuoc.TimTheoMa(item.MaThuoc).GiaBan;
            }
            return tongTien;
        }

        public void XuatHD()
        {
            //Console.Write("\n   " + maHoaDon + "\t" + maNhanVien + "\t" + maKhachHang + "\t" + ngayLap + "\t");
            Console.Write("\n\t" + maHoaDon);
            NhanVien nv = DanhSachNhanVien.TimNVTheoMa(maNhanVien);
            if(nv != null)
                Console.Write("\t" + nv.HoTenNV);
            KhachHang KH = DanhSachKhachHang.TimTheoMa(maKhachHang);
            if(KH != null)
                Console.Write("\t" + KH.HoTen);
            Console.Write(string.Format("{0:dd/MM/yyyy}", ngayLap).PadLeft(23));
            Console.Write(thanhTien().ToString().PadLeft(10));
        }

        public void ghiFile()
        {
            FileStream file = new FileStream("DSHoaDon.txt", FileMode.Append);
            BinaryWriter br = new BinaryWriter(file);
            br.Write(maHoaDon);
            br.Write(maNhanVien);
            br.Write(maKhachHang);
            br.Write(String.Format("{0:dd/MM/yyyy}", ngayLap));//thanhTien?
            br.Close();
            file.Close();
        }
    }
}
